<?php
namespace app\admin\controller;
use app\common\model\Article as ArticleModel;
use app\common\model\Cate as CateModel;
use app\common\validate\ArticleValidate;
use think\Session;

class Article extends AdminCommon
{
    public function index(){
        $params = $this->request->param();
        $where = [];
        if(!empty($params['search_field']) && !empty($params['search_value'])){
            $where[$params['search_field']] = $params['search_value'];
        }
        $articleModel = new ArticleModel();
        $list = $articleModel->alias('a')->join('cate b','a.cate_id=b.id','LEFT')->field('a.*,b.cate_name')->where($where)->order(['a.id'=>'desc','a.article_sort'=>'desc'])->paginate();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 添加文章
     * @desc 添加文章 
     */
    public function add(){
        $params = $this->request->param();
        if($this->request->isAjax()&&$this->request->isPost()&&isset($params['req_sub'])){
            if(!isset($params['content'])){
                $params['content'] = '';
            }
            $params['add_time'] = date('Y-m-d H:i:s',time());
            //校验器
            $articleValidate = new ArticleValidate();
            if(!$articleValidate->scene('add')->check($params)){
                $this->error($articleValidate->getError());
            }

            $articleModel = new ArticleModel($params);
            if($articleModel->allowField(true)->save()){
                $this->success('发布成功','admin/article/index');
            }else{
                $this->error('发布失败');
            }
        }else{
            $cateModel = new CateModel();
            $this->assign('parentCateList',$cateModel->getCateChoicesList());
            return $this->fetch();
        }
    }


    /**
     * 编辑文章
     * @desc 编辑文章
     */
    public function edit($id)
    {
        $params = $this->request->param();
        if($this->request->isAjax()&&$this->request->isPost()&&isset($params['req_sub'])){
            if(!isset($params['content'])){
                $params['content'] = '';
            }
            $params['edit_time'] = date('Y-m-d H:i:s',time());
            //校验器
            $articleValidate = new ArticleValidate();
            if(!$articleValidate->scene('edit')->check($params)){
                $this->error($articleValidate->getError());
            }

            $articleModel = new ArticleModel();
            if($articleModel->allowField(true)->save($params,['id'=>$id]) !== false){
                $this->success('编辑成功','admin/article/index');
            }else{
                $this->error('编辑失败');
            }
        }else{
            $cateModel = new CateModel();
            $this->assign('parentCateList',$cateModel->getCateChoicesList());
            $this->assign('info',ArticleModel::get($id));
            return $this->fetch();
        }
    }

    /**
     * 删除文章
     * @desc 删除文章
     */
    public function del($id)
    {
        if ($this->request->isAjax()) {
            $articleModel = new ArticleModel();
            if ($articleModel->where(['id' => $id])->delete() === false) {
                $this->error('删除失败');
            } else {
                $this->success('删除成功', 'admin/article/index');
            }
        }
    }
}